package com.sc.admin.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sc.admin.common.ResponseResult;
import com.sc.admin.pojo.Role;
import com.sc.admin.service.RoleService;

@Controller
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/page")
	public String rolePage() {
		return "role-list";
	}
	
	@RequestMapping("/list")
	public @ResponseBody Object roleList(int page,int rows) {
		int count = roleService.getCount();
		List<Role> roles = roleService.getRoles(page, rows);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("total", count);
		map.put("rows", roles);
		return map;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String addPage() {
		return "role-add";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public @ResponseBody Object add(Role role) {
		roleService.add(role);
		return new ResponseResult().ok();
	}
	
	@RequestMapping(value="/name/check",method=RequestMethod.GET)
	public @ResponseBody Object check(String name) {
		Role role = roleService.checkName(name);
		return new ResponseResult().ok(role);
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public @ResponseBody Object edit(Role role) {
		roleService.update(role);
		return new ResponseResult().ok();
	}
}
